import {Component, OnInit} from "@angular/core";
import {Page} from "../../../public/util/page";
import {MinerService} from "../miner.service";
import {Enums, MinerStates, States, YesOrNo} from '../../../public/setting/enums';
import {SettingUrl} from "../../../public/setting/setting_url";

@Component({
  selector: 'app-miner-item-list',
  templateUrl: './miner-item-list.component.html',
  styleUrls: ['./miner-item-list.component.scss']
})
export class MinerItemListComponent implements OnInit {
  public searchParams: any = {};//搜索参数
  public minerItemList: Page = new Page(); //供应商
  public _loading: boolean = false;
  public routerLinks = SettingUrl.ROUTERLINK;//路由
  public states = Enums.minerItemState;       // 状态编码
  public minerStates = MinerStates;       // 状态值枚举
  public isActiveCoin = Enums.yn;       // 状态
  public yesOrNo = YesOrNo;       // 是否状态

  constructor(private minerService: MinerService) {
  }

  ngOnInit() {
    this.queryMinerItemList()
  }

  /**
   * 查询minerItem列表
   * @param curPage 当前页
   */
  queryMinerItemList(curPage?: number) {
    this._loading = true;
    if (curPage) this.minerItemList.curPage = curPage;//当有页码时，查询该页数据
    this.minerItemList.params = {
      curPage: this.minerItemList.curPage, //目标页码
      pageSize: this.minerItemList.pageSize, //每页条数
    };
    this.minerService.getMinerItemList(this.minerItemList.params).then((res: Page) => {
      this._loading = false;
      this.minerItemList = res;
    }).catch(err => {
      this._loading = false;
    })
  }

  /**
   * 修改禁用状态
   * @param code
   * @param event:boolean
   */
  modifyMinerItemState(code, event) {
    let state = event ? States.enable : States.disable;//转换为对应的枚举
    this.minerService.modifyMinerItemState(code, state).catch((res) => {
      this.queryMinerItemList();//由于switch的特殊性，因此在失败的时候刷新页面
    })
  }

  /**
   * 重置搜索
   */
  resetSearch() {
    this.searchParams = {};
    this.queryMinerItemList(1)
  }
}
